package com.jeehentor.generate.generate.fileGenrate;

import com.jeehentor.generate.dto.GenerateFileDTO;
import org.apache.velocity.app.VelocityEngine;

import java.util.List;
import java.util.Properties;

import static com.jeehentor.generate.generate.constants.GenerateFileType.JAVA_FILE;
import static com.jeehentor.generate.generate.constants.GenerateFileType.VUE_FILE;


/**
 * 代码文件生成工厂类
 */
public class FileGenerateFactory {


    public static List<String> generate(GenerateFileDTO generateFileDTO) {
        //初始化Velocity引擎
        VelocityEngine velocityEngine = new VelocityEngine();
        Properties properties = new Properties();
        properties.setProperty("resource.loaders", "classpath");
        properties.setProperty("resource.loader.classpath.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        properties.setProperty("resource.loader.classpath.cache", "true");
        properties.setProperty("resource.loader.classpath.modificationCheckInterval", "0");
        velocityEngine.init(properties);
        generateFileDTO.setVelocityEngin(velocityEngine);
        AbstractFileGenerate generate;
        switch (generateFileDTO.getFileType()) {
            case JAVA_FILE:
                generate = new JavaFileGenerate();
                break;
            case VUE_FILE:
                generate = new VueFileGenerate();
                break;
            default:
                throw new RuntimeException(String.format("未找到文件类型 %s 的生成类", generateFileDTO.getFileType()));
        }
        return generate.generate(generateFileDTO);
    }

}
